/******************************************************************************
 Copyright (c) 2022-2025 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

 REVISION:      $Revision: #14 $
******************************************************************************/

#ifndef GATT_XPAN_SERVER_COMMON_H
#define GATT_XPAN_SERVER_COMMON_H

#include "csr_synergy.h"

#ifndef EXCLUDE_GATT_XPAN_SERVER_MODULE

#include "gatt_xpan_server_private.h"
#include "csr_bt_gatt_lib.h"
#include "csr_pmem.h"

#define GATT_XPAN_SERVER_CCC_NOTIFY                   (0x01)
#define GATT_XPAN_SERVER_CCC_INDICATE                 (0x02)

#define XPAN_SERVER_CHECK_BITS_VALID(x, shift) (!(x >> shift))
#define XPAN_SERVER_CHECK_FEATURES_BITS_VALID(ptr) ((ptr[2] >> XPAN_SERVER_SERVER_FEATURES_VALID_BITS == 0) && ((ptr[3] || ptr[4] || ptr[5] || ptr[6] || ptr[7]) == 0))

/* Required octets for the Client Configuration Characteristic */
#define GATT_XPAN_SERVER_CCC_VALUE_SIZE                         (2)

/* Size of Ip address characteristics for num Ip Addresses and Ssid Length in octets */
#define GATT_XPAN_SERVER_IP_ADDRESS_FIXED_SIZE                  (2)

/* Required octets for the L2CAP TCP Port Characteristic */
#define GATT_XPAN_SERVER_L2CAP_TCP_PORT_SIZE                    (2)

/* Required octets for the UDP Port Characteristic */
#define GATT_XPAN_SERVER_UDP_PORT_SIZE                          (2)

/* Required octets for the mDNS Srv Uuid Characteristic */
#define GATT_XPAN_SERVER_MDNS_SRV_UUID_SIZE                     (16)

/* Required octets for the Country String Characteristic *
Already defined in gatt_xpan_server.h
#define GATT_XPAN_SERVER_COUNTRY_STRING_SIZE                    (3)*/

/* Required octets for the Bearer Preference Characteristic */
#define GATT_XPAN_SERVER_BEARER_PREFERENCE_SIZE                 (2)

/* Required octets for the MAC Address Tuple Characteristic */
#define GATT_XPAN_SERVER_MAC_ADDRESS_T_SIZE                     (10)

/* Required octets for the Requested SAP Power State Characteristic */
#define GATT_XPAN_SERVER_REQUESTED_SAP_POWER_STATE_SIZE         (1)

/* Required octets for the Twt Configuration Characteristic */
#define GATT_XPAN_SERVER_TWT_CONFIGURATION_SIZE                 (22)

/* Required octets for the Clear To Send Characteristic */
#define GATT_XPAN_SERVER_CLEAR_TO_SEND_SIZE                     (1)

/* Minimum fixed size of the XPAN Control point in case of Connect SSID opcode */
#define GATT_XPAN_SERVER_CONNECT_SSID_OPCODE_SIZE              (16)

/* Minimum fixed size of the XPAN Control point in case of Disconnect SSID opcode */
#define GATT_XPAN_SERVER_DISCONNECT_SSID_OPCODE_SIZE           (2)

/* Minimum fixed size of the XPAN Control point in case of Connected SSID opcode */
#define GATT_XPAN_SERVER_CONNECTED_SSID_OPCODE_SIZE            (32)

/* Minimum fixed size of the XPAN Control point in case of Available SSID opcode */
#define GATT_XPAN_SERVER_AVAILABLE_SSID_OPCODE_SIZE            (5)

/* Minimum fixed size of the XPAN Control point in case of Related SSIDs opcode */
#define GATT_XPAN_SERVER_RELATED_SSIDS_OPCODE_SIZE             (2)

/* Minimum fixed size of the XPAN Control point in case of mDns Srv UUID SSID opcode */
#define GATT_XPAN_SERVER_MDNS_SRV_UUID_OPCODE_SIZE             (17)

/* Minimum fixed size of the XPAN Control point in case of Remove SSID opcode */
#define GATT_XPAN_SERVER_REMOVE_SSID_OPCODE_SIZE               (2)

/* Minimum fixed size of the XPAN Control point in case of L2CAP TCP port opcode */
#define GATT_XPAN_SERVER_L2CAP_TCP_PORT_OPCODE_SIZE            (3)

/* Minimum fixed size of the XPAN Control point in case of UDP port opcode */
#define GATT_XPAN_SERVER_UDP_PORT_OPCODE_SIZE                  (5)

/* Minimum fixed size of the XPAN Control point in case of ether type opcode */
#define GATT_XPAN_SERVER_ETHER_TYPE_OPCODE_SIZE                (3)

/* Minimum fixed size of the XPAN Control point in case of MAC address opcode */
#define GATT_XPAN_SERVER_MAC_ADDRESS_OPCODE_SIZE               (7)

/* Minimum fixed size of the XPAN Control point in case of Bearer preference opcode */
#define GATT_XPAN_SERVER_BEARER_PREFERENCE_OPCODE_SIZE         (2)

/* Minimum fixed size of the XPAN Control point in case of udp sync port opcode */
#define GATT_XPAN_SERVER_UDP_SYNC_PORT_OPCODE_SIZE             (3)

/* Fixed size of the XPAN Control point in case of roaming request opcode */
#define GATT_XPAN_SERVER_ROAMING_REQUEST_OPCODE_SIZE           (1)

/* Fixed size of the XPAN Control point in case of multicast mac address opcode */
#define GATT_XPAN_SERVER_MULTICAST_MAC_ADDRESS_OPCODE_SIZE     (7)

/* Fixed size of the XPAN Control point in case of use case identifier opcode */
#define GATT_XPAN_SERVER_USE_CASE_IDENTIFER_OPCODE_SIZE        (4)

/* Fixed size of the XPAN Control point in case of Wi-Fi channel switch response opcode */
#define GATT_XPAN_SERVER_WIFI_CHANNEL_SWITCH_RESPONSE_OPCODE_SIZE (2)

/* Fixed size of the XPAN Control point in case of bearer switch opcode */
#define GATT_XPAN_SERVER_BEARER_SWITCH_FAILED_OPCODE_SIZE      (3)

/* Fixed size of the XPAN Control point in case of country string opcode */
#define GATT_XPAN_SERVER_COUNTRY_STRING_OPCODE_SIZE            (4)

/* Fixed size of the XPAN Control point in case of Sap State opcode */
#define GATT_XPAN_SERVER_SAP_STATE_OPCODE_SIZE                 (3)

/* Minimum fixed size of the XPAN Control point in case of Update beacon parameters opcode */
#define GATT_XPAN_SERVER_UPDATE_BEACON_PARAMETERS_OPCODE_SIZE  (13)

/* Required Octets for Next TBTT TSF in Update Beacon Parameters opcode */
#define XPAN_NEXT_TBTT_TSF_SIZE                                (8)

/* Minimum fixed size of the XPAN Control point in case of SAP power state response opcode */
#define GATT_XPAN_SERVER_SAP_POWER_STATE_RESPONSE_OPCODE_SIZE   (2)

/* Size of an Client features opcode in octets */
#define XPAN_SERVER_CLIENT_FEATURES_OPCODE_SIZE                 (9)

/* Size of an Audio bearer switch request opcode in octets */
#define XPAN_SERVER_AUDIO_BEARER_SWITCH_REQUEST_OPCODE_SIZE     (2)

/* Size of an Bssid struct in octets */
#define XPAN_SERVER_BSSID_STRUCT_SIZE                           (8)

/* Size of an ipv4 address in octets */
#define XPAN_SERVER_IPV4_ADDRESS_SIZE                           (4)

/* Size of an audio Location in octets */
#define XPAN_SERVER_AUDIO_LOCATION_SIZE                         (4)

/* Ipv4AddressData attribute size in octets */
#define XPAN_SERVER_IPV4_ADDRESS_DATA_STRUCT_SIZE               (10)

/* Size of an ipv6 address in octets */
#define XPAN_SERVER_IPV6_ADDRESS_SIZE                           (16)

/* Ipv6AddressData attribute size in octets */
#define XPAN_SERVER_IPV6_ADDRESS_DATA_STRUCT_SIZE               (22)

/* Size of an Server features in octets */
#define XPAN_SERVER_SERVER_FEATURES_SIZE                        (8)

/* Valid bits to be set in Server Features characteristic
*  Note: This value is calculated by performing % on the number of valid
*        bits with octet length in bits(8). Currently 21 bits are valid
*        as such we get 21%8 == 5.
*/
#define XPAN_SERVER_SERVER_FEATURES_VALID_BITS                  (5)

/* Valid bits to be set the bitfield of the TWT configuration characteristic */
#define XPAN_SERVER_TWT_CONFIG_BITFIELD_VALID_BITS              (3)

/* Size of an Audio bearer switch response in octets */
#define XPAN_SERVER_AUDIO_BEARER_SWITCH_RESPONSE_SIZE           (1)

/* Valid bits to be set in Audio bearer switch response characteristic */
#define XPAN_SERVER_AUDIO_BEARER_SWITCH_RESPONSE_VALID_BITS     (1)

/* Size of the Num Devices Present characteristic in octets */
#define XPAN_SERVER_NUM_DEVICES_PRESENT_SIZE                    (1)

/* Minimum valid value for the numDevices for the Num Devices Present characteristic */
#define XPAN_SERVER_NUM_DEVICES_PRESENT_MIN                     (1)

/* Size of the Voice back channel periodicity characteristic in octets */
#define XPAN_SERVER_VOICE_CHANNEL_PERIODICITY_SIZE              (1)

/* Size of the Request WiFi Scan Results characteristic in octets */
#define XPAN_SERVER_REQUEST_WIFI_SCAN_RESULTS_SIZE              (0)

/* Minimum valid value for the periodicity for the Voice back channel periodicity characteristic */
#define XPAN_SERVER_PERIODICITY_MIN                             (1)

/* Size of the TWT Status characteristic in octets */
#define XPAN_SERVER_TWT_STATUS_SIZE                             (4)

/* Size of the SAP Connection Status characteristic in octets */
#define XPAN_SERVER_SAP_CONNECTION_STATUS_SIZE                  (4)

/* ConnectedDevicesData attribute size in octets */
#define XPAN_SERVER_CONNECTED_DEVICES_DATA_SIZE                 (3)

/* XPAN Server invalid cid index value */
#define GATT_XPAN_SERVER_INVALID_CID_INDEX                      (0xFF)

/* XPAN Server maximum length of SSID */
#define GATT_XPAN_SERVER_MIN_SSID_SIZE                          (1)

/* XPAN Server maximum length of SSID */
#define GATT_XPAN_SERVER_MAX_SSID_SIZE                          (32)

/* XPAN characteristic audio location mask */
#define GATT_XPAN_SERVER_AUDIO_LOCATION_MAX                     (0x0FFFFFFFu)

/* XPAN Server minimum length of passphrase length */
#define GATT_XPAN_SERVER_MIN_PASSPHRASE_LENGTH_SIZE              (8)

/* XPAN Server maximum length of passphrase length */
#define GATT_XPAN_SERVER_MAX_PASSPHRASE_LENGTH_SIZE              (63)

/* Size of the Roaming Request Response characteristic in octets */
#define XPAN_SERVER_ROAMING_REQUEST_RESPONSE_SIZE                (1)

/* Size of the WiFi Channel Switch Request characteristic in octets */
#define XPAN_SERVER_WIFI_CHANNEL_SWITCH_REQUEST_SIZE             (2)

/*! @brief Enumeration of opcodes an application task can receive from the
*          GATT XPAN Server
*/
#define GATT_XPAN_SERVER_CONNECT_SSID_OPCODE                  0x01
#define GATT_XPAN_SERVER_DISCONNECT_SSID_OPCODE               0x02
#define GATT_XPAN_SERVER_CONNECTED_SSID_OPCODE                0x03
#define GATT_XPAN_SERVER_AVAILABLE_SSID_OPCODE                0x04
#define GATT_XPAN_SERVER_RELATED_SSIDS_OPCODE                 0x05
#define GATT_XPAN_SERVER_MDNS_SRV_UUID_OPCODE                 0x06
#define GATT_XPAN_SERVER_REMOVE_SSID_OPCODE                   0x07
#define GATT_XPAN_SERVER_L2CAP_TCP_PORT_OPCODE                0x08
#define GATT_XPAN_SERVER_UDP_PORT_OPCODE                      0x09
#define GATT_XPAN_SERVER_ETHER_TYPE_OPCODE                    0x0A
#define GATT_XPAN_SERVER_MAC_ADDRESS_OPCODE                   0x0B
#define GATT_XPAN_SERVER_BEARER_PREFERENCE_RESPONSE_OPCODE    0x0C
#define GATT_XPAN_SERVER_UPDATE_BEACON_PARAMETERS_OPCODE      0x0D
#define GATT_XPAN_SERVER_SAP_POWER_STATE_RESPONSE_OPCODE      0x0E
#define GATT_XPAN_SERVER_CLIENT_FEATURES_OPCODE               0x0F
#define GATT_XPAN_SERVER_AUDIO_BEARER_SWITCH_REQUEST_OPCODE   0x10
#define GATT_XPAN_SERVER_WIFI_SCAN_RESULTS_OPCODE             0x11
#define GATT_XPAN_SERVER_CHANNEL_SWITCH_ANNOUNCEMENT_OPCODE   0x12
#define GATT_XPAN_SERVER_UDP_SYNC_PORT_OPCODE                 0x13
#define GATT_XPAN_SERVER_ROAMING_REQUEST_OPCODE               0x14
#define GATT_XPAN_SERVER_MULTICAST_MAC_ADDRESS_OPCODE         0x15
#define GATT_XPAN_SERVER_USECASE_IDENTIFIER_OPCODE            0x16
#define GATT_XPAN_SERVER_AP_CONNECT_ASSIST_OPCODE             0x17
#define GATT_XPAN_SERVER_WIFI_CHANNEL_SWITCH_RESPONSE_OPCODE  0x18
#define GATT_XPAN_SERVER_BEARER_SWITCH_FAILED_OPCODE          0x19
#define GATT_XPAN_SERVER_COUNTRY_STRING_OPCODE                0x1A
#define GATT_XPAN_SERVER_SAP_STATE_OPCODE                     0x1B
#define GATT_XPAN_SERVER_CONNECTED_ENTERPRISE_SSID_OPCODE     0x1C
#define GATT_XPAN_SERVER_OPCODE_TOP                           0x1D


/**************************************************************************
NAME
    xpanServerSendAccessRsp

DESCRIPTION
    Send an access response to the GATT Manager library.
*/
void xpanServerSendAccessRsp(
        CsrBtGattId task,
        ConnectionId cid,
        uint16 handle,
        uint16 result,
        uint16 sizeValue,
        uint8 *const value);

/**************************************************************************
NAME
    xpanServerSendAccessErrorRsp

DESCRIPTION
    Send an access response to the GATT Manager library with an error status.
*/

#define xpanServerSendAccessErrorRsp(task, cid, handle, error) \
    xpanServerSendAccessRsp( \
            task, \
            cid, \
            handle, \
            error, \
            0, \
            NULL \
            )

/**************************************************************************
NAME
    xpanServerWriteGenericResponse

DESCRIPTION
    Send a generic response to the clien after a writing.
*/
void xpanServerWriteGenericResponse(
        CsrBtGattId task,
        ConnectionId cid,
        uint16      result,
        uint16      handle);

/***************************************************************************
NAME
    xpanServerSendCharacteristicChangedNotification

DESCRIPTION
    Send a notification to the client to notify that the value of
    a characteristic is changed.
*/
void xpanServerSendCharacteristicChangedNotification(
        CsrBtGattId  task,
        ConnectionId cid,
        uint16 handle,
        uint16 sizeValue,
        uint8 *const value);

/***************************************************************************
NAME
    xpanServerHandleReadClientConfigAccess

DESCRIPTION
    Deals with access of the characteristic client config in case of reading.
*/
void xpanServerHandleReadClientConfigAccess(
        CsrBtGattId  task,
        ConnectionId cid,
        uint16 handle,
        const uint16 clientConfig);

/***************************************************************************
NAME
    xpanServerHandleWriteClientConfigAccess

DESCRIPTION
    Deals with the access of a client config handle to be written and indicated
    to the application.
*/
void xpanServerHandleWriteClientConfigAccess(
        GattXpanServerData *gattXpanServerInstance,
        CsrBtGattDbAccessWriteInd *const accessInd);

/***************************************************************************
NAME
    xpanCompareStoredSsids

DESCRIPTION
    Compares the the passed StoredSsid List with the StoredSsid characteristic.
    Returns TRUE if they are the same and FALSE otherwise.
*/
bool xpanCompareStoredSsids(GattXpanServerData *gattXpanServerInstance, const StoredSsid *storedSsid);

/***************************************************************************
NAME
    xpanServerStoredSsidSize

DESCRIPTION
    Calculates the length of the Stored SSIDs characteristic in octets
    and returns it.
*/
uint16 xpanServerStoredSsidSize(GattXpanServerData *gattXpanServerInstance);

/***************************************************************************
NAME
    xpanServerComposeStoredSsidValue

DESCRIPTION
    Converts the Stored SSIDs characteristic into an array of uint8s and
    stores it in value.
*/
uint8* xpanServerComposeStoredSsidValue(uint16 *characteristicLength,
                                       GattXpanServerData *gattXpanServerInstance);

/***************************************************************************
NAME
    xpanServerGetIpv4AddressCharacteristicSize

DESCRIPTION
    Calculates the size of the Ipv4 address characteristic and returns it.
*/
uint16 xpanServerGetIpv4AddressCharacteristicSize(const GattXpanServerData *gattXpanServerInstance);

/***************************************************************************
NAME
    xpanServerComposeIpv4AddressValue

DESCRIPTION
    Converts the Ipv4 addresses characteristic into an array of uint8s and
    stores it in value.
*/
uint8* xpanServerComposeIpv4AddressValue(uint16 *characteristicLength,
                                        const GattXpanServerData *gattXpanServerInstance);

/***************************************************************************
NAME
    xpanServerGetIpv6AddressCharacteristicSize

DESCRIPTION
    Calculates the size of the Ipv6 address characteristic and returns it.
*/
uint16 xpanServerGetIpv6AddressCharacteristicSize(const GattXpanServerData *gattXpanServerInstance);

/***************************************************************************
NAME
    xpanServerComposeIpv6AddressValue

DESCRIPTION
    Converts the Ipv6 addresses characteristic into an array of uint8s and
    stores it in value.
*/
uint8* xpanServerComposeIpv6AddressValue(uint16 *characteristicLength,
                                        const GattXpanServerData *gattXpanServerInstance);

/***************************************************************************
NAME
    xpanServerComposeTwtConfigurationValue

DESCRIPTION
    Converts the TwtConfiguration characteristic into an array of uint8s and
    stores it in value.
*/
uint8* xpanServerComposeTwtConfigurationValue(uint16* characteristicLength,
                                             GattXpanServerData *gattXpanServerInstance,
                                             ConnectionId cid);

/***************************************************************************
NAME
    xpanServerComposeConnectedDevicesValue

DESCRIPTION
    Converts the Connected devices characteristic into an array of uint8s and
    stores it in value.
*/
uint8* xpanServerComposeConnectedDevicesValue(uint16* characteristicLength,
                                             GattXpanServerData *gattXpanServerInstance);

/***************************************************************************
NAME
    xpanServerComposeBearerPreferenceValue

DESCRIPTION
    Converts the Bearer Preference characteristic into an array of uint8s and
    stores it in value.
*/
uint8* xpanServerComposeBearerPreferenceValue(uint16 *characteristicLength, GattXpanServerData *gattXpanServerInstance);

/***************************************************************************
NAME
    xpanServerComposeMacAddressValue

DESCRIPTION
    Converts the MAC addresses characteristic into an array of uint8s and
    stores it in value.
*/
uint8* xpanServerComposeMacAddressValue(uint16 *characteristicLength,
                                       GattXpanServerData *gattXpanServerInstance);

#endif /* EXCLUDE_GATT_XPAN_SERVER_MODULE */

#endif /* GATT_XPAN_SERVER_COMMON_H */
